package com.nsgf.library.ui.page;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.MainNsgfActivity;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.databinding.ActivityNsgfModifyPassBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.indicatorlib.utils.Utils;
import com.nsgf.library.utils.BaseAlterDialog;
import com.nsgf.skjz.utils.ToastUtils;

import java.util.Objects;

public class ModifyPassWordActivity extends BaseActivity {
    private ModifyPassWordState mStates;


    private Dialog dialog;

    private ActivityNsgfModifyPassBinding mBinding;

    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(ModifyPassWordState.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_nsgf_modify_pass, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mBinding = (ActivityNsgfModifyPassBinding) getBinding();
        mStates.titleBar.set("修改密码");


        mAccountRequester.getSrc().observe(this, dataResult -> {


            if (dataResult.getResponseStatus().isSuccess()) {
                if (200 == dataResult.getResult().getResponseCode()) {
                    ToastUtils.showLongToast("修改成功");
                    dialog.dismiss();
                } else {
                    getController().dismissLoading();
                    new BaseAlterDialog(this, R.style.dialogstyle,
                            dataResult.getResult().getMsg(), "确定", "修改失败",
                            (dialog, confirm) -> {
                                dialog.dismiss();

                            }).show();
                }
            } else {
                ToastUtils.showLongToast(getErrorMsg(dataResult.getResponseStatus().getE()));
            }

        });


        mAccountRequester.getSrcSuccess().observe(this, new Observer<ResponseStatus<String>>() {
            @Override
            public void onChanged(ResponseStatus<String> responseStatus) {
                if (200 == responseStatus.getResponseCode()) {
                    finish();
                } else {
                    if (401 == responseStatus.getResponseCode()) {
                        UserInfo.getInstance().clearUserInfo();
                        finish();
                    }
                    ToastUtils.showLongToast(responseStatus.getMsg());
                }
            }
        });
    }

    public static class ModifyPassWordState extends StateHolder {

        public final State<String> oldPass = new State<>("");
        public final State<String> newPass = new State<>("");
        public final State<String> confirmNewPass = new State<>("");
        public final State<String> name = new State<>("");

        public final State<String> password = new State<>("");

        public final State<Boolean> loadingVisible = new State<>(false);

    }

    public static class LoginDialogStates extends StateHolder {

        public final State<String> name = new State<>("123123");

    }


    public class ClickProxy {

        public void login() {

            //TODO tip 5：通过双向绑定，使能通过 state-ViewModel 中与 xml 控件发生绑定的"可观察数据" 拿到控件数据，
            // 避免直接接触控件实例而埋下 Null 安全一致性隐患。

            if (TextUtils.isEmpty(mStates.oldPass.get()) || TextUtils.isEmpty(mStates.newPass.get())
                    || TextUtils.isEmpty(mStates.confirmNewPass.get())) {
                ToastUtils.showLongToast(getString(R.string.username_or_pwd_incomplete));
                return;
            }
            if (!Objects.equals(mStates.newPass.get(), mStates.confirmNewPass.get())) {
                ToastUtils.showLongToast("新密码两次不一致");
                return;
            }

            if (!Utils.isPasswordSecure(mStates.newPass.get())){
                ToastUtils.showLongToast("8-18位,字母/数字/特殊字符!@#$%^&*");
                return;
            }

            mAccountRequester.updatePassWord(mStates.oldPass.get(),
                    mStates.oldPass.get(),
                    mStates.newPass.get(),
                    mStates.confirmNewPass.get());
        }
    }


    public class DialogClickProxy {
        public void cancel() {
            dialog.dismiss();
        }

        public void confirm() {
            dialog.dismiss();
            startActivity(new Intent().setClass(ModifyPassWordActivity.this, MainNsgfActivity.class));
        }
    }
}
